var QQMapWX = require('../qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  onLoad: function (options){
    var model=JSON.parse(options.model);
    this.setData({
      title:model.title,
      color:model.color,
      com:model.com,
      version: model.version,
      img:model.img,
      choose_num:model.num,
      money:model.money
    }),
    this.calculation('up')
    qqmapsdk = new QQMapWX({
      key: 'TBMBZ-3G7CW-FECRA-REKKI-Q7MNT-P7F6W'
    })
  },
  data: {
    store_logo:'../img/店铺.png',
    store_title:['官方自营店'],
    title:'',
    color:'',
    version:'',
    com:'',
    img:'',
    choose_num:'',
    money:'',
    total_number:'',
    freight:'0',
    this_username:'请点击输入当前送递信息',
    this_address:'无法获取当前位置',
  },
  up: function (e) {
    this.setData({
      choose_num: this.data.choose_num + 1
    })
    this.calculation('up')
  },
  down: function (e) {
    if (this.data.choose_num > 1) {
      this.setData({
        choose_num: this.data.choose_num - 1
      })
    }
    this.calculation('down')
  },
  calculation:function(str){
    if(str=='up'){
      var money = this.data.money;
      var num = this.data.choose_num;
      var total_number = money * num;
      this.setData({
        total_number:total_number
      })
    }else{
      var money = this.data.money;
      var num = this.data.choose_num;
      var total_number = money/num;
      this.setData({
        total_number: total_number
      })
    }
    
  },
  user: function () {
    var then=this
    wx.getUserInfo({
        success(res){
          var name=res.userInfo.nickName
          then.setData({
            this_username:name
          })
          then.address()
        }
      })
  },
  play:function(){
    var then=this
    var name = this.data.this_username
    var address = this.data.this_address
    if (name === '请点击输入当前送递信息' || address === '无法获取当前位置') {
      wx.showToast({
        title: '请点击送递信息',
        icon: 'none'
      })
    }else{
      wx.showModal({
        title: '订单确认',
        content: '确认下单',
        success: function (res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '../buys_success/buys_success'
            })
            console.log('确认')
          } else if (res.cancel) {
            console.log('取消')
          }
        }
      })
    }
   
  },
  address:function(){
    var then=this
    wx.authorize({
      scope: 'scope.userLocation',
      success() {
        // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
        wx.getLocation({
          success: function (res) {
            var latitude_this = res.latitude;
            var longitude_this = res.longitude;
            console.log(latitude_this)
            qqmapsdk.reverseGeocoder({
              location: {
                latitude: latitude_this,
                longitude: longitude_this
              },
              success: function (res) {
                then.setData({
                  this_address:res.result.address
                })
              },
              fail: function (res) {
                console.log(res);
              },
            });
          }
        })
      }
    })
  }
  
})